//*****************************************************************************
//  172x72 COG Display Temp. Comp. FW
//  MCU: MSP430F235
//  PCB: 10424
//  Compiler: TI Code Composer Essentials MSP430 v3.1
//  Kent Displays, Inc.
//  Copyright (c) 2009. All rights reserved worldwide.
//  Revisions
//     A - Initial. (24V Drive System, No Focal Conic Erase)
//     B - 28V Drive System, Focal Conic Erase
//*****************************************************************************

#include "system.h"
#include "mystring.h" 
#include "hal_ucs.h"

#define UART_DEBUG
#define RTC_DEBUG
#define RF_DEBUG
//#define LCD_DEBUG
#define LED_DEBUG
//#define GT_DEBUG

#ifdef GT_DEBUG
#include "GT22.h"
#endif

#ifdef LED_DEBUG
#include "led.h" 
#endif

#ifdef LED_DEBUG
#define LED1_RESET() LED1_reset()
#define LED2_RESET() LED2_reset()
#define LED1_TOGGLE() LED1_toggle()
#define LED2_TOGGLE() LED2_toggle()
#define LED1_SET() LED1_set()
#define LED2_SET() LED2_set()
#else
#define LED1_RESET()
#define LED2_RESET() 
#define LED1_TOGGLE()
#define LED2_TOGGLE() 
#define LED1_SET() 
#define LED2_SET()
#endif

#ifdef RF_DEBUG
#include "CC2500.h"
#endif

#ifdef RTC_DEBUG
#include "rtc.h"
#endif

#ifdef UART_DEBUG
#include "uart.h"
#define PRINT_CHAR(x) print_char(x) 
#define PRINT(x) print(x)
#define PRINT_RF(x, length) print_rf(x, length)
#else
#define PRINT_CHAR(x)
#define PRINT(x)
#define PRINT_RF(x, length)
#endif

/********** reply *************/

#define REPLY_OK 0x00
#define REPLY_ERR 0x01
#define REPLY_LOSE 0x03

enum { UPDATETYPE = 0x64, TIMETYPE = 0x65, INTERVALTYPE = 0x66 , WORKTIMETYPE = 0x67, ESLADDRESSTYPE = 0x68 };
unsigned char MessageType;

typedef enum {  LPM4_MODE, LPM3_MODE, ACTIVE_MODE, APPLICATION_MODE } McuStatus;
volatile McuStatus CpuMode = ACTIVE_MODE;

typedef enum {  TURE = 0, FALSE = !TURE } FlagStatus;

volatile FlagStatus timeFlag = TURE;
volatile FlagStatus uptimeFlag = TURE;

#ifdef UART_DEBUG
volatile FlagStatus uart_receive_flag = TURE;
#endif

volatile FlagStatus interrup_time = TURE;

#ifdef RF_DEBUG
volatile FlagStatus rf_receive_flag = TURE;
#endif

Calendar baseTime;

Calendar activeTime = {0x00, 0x03, 0x09, 0x00, 0x26, 0x09, 0x2012};

Calendar sleepTime;
volatile Calendar newTime;

unsigned char TxBuf_OK[10] = { 0x09, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x00, 0x92 };
unsigned char TxBuf_ERR[10] = { 0x09, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x01, 0x91 };
unsigned char TxBuf_LOSE[10] = {0x0a, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

// Global variables.
volatile unsigned int gTimer;   // Used to extend HW 16 bit counter to 32 bits.

unsigned char obj_addrLOCAL = 0x01;

unsigned char rxBuffer[64];

void handMessageType(unsigned char messagetype);

unsigned char sum_buffer[300] = {0x00};

void main(void)
{   
	// System Initialization
	StopWatchdog();               // Stop watchdog timer.
	ConfigPorts();                // Configure I/O ports.
//	ConfigClocks();               // Configure basic clock module.
    
    P5SEL |= BIT4 + BIT5;                       // Port select XT1
 
	//Initialize clock and peripherals
	LFXT_Start(XT1DRIVE_3, XCAP_3);
    Init_FLL_Settle(8000, 244);
    SFRIE1 |= OFIE;

#ifdef LED_DEBUG    
    LED_config();
#endif
    
#ifdef UART_DEBUG
     UART_config();
     PRINT("hello world!");
#endif

#ifdef RF_DEBUG
    SPI_SELECT();
    SPI_SET_CLK();
    SPI_DIS_SELECT();
    cc2500_Init(0x00);
    Set_CCxx00Rxd();
#endif

#ifdef RF_DEBUG

    P2IES |= 0x01;
    P2IFG &= ~0x01;
    P2IE  |= 0x01;
#endif

    _BIS_SR(GIE);
	
	CpuMode = ACTIVE_MODE;

	while (1) {
		
	}
}


#ifdef UART_DEBUG

unsigned char k = 0x00;
unsigned char c = 0x00;

#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR (void)
{
    switch (__even_in_range(UCA1IV,4)){
        //Vector 2 - RXIFG
        case 2:

            //USCI_A0 TX buffer ready?
            while (!UART_getInterruptStatus(UCTXIFG));
            c = UART_receiveData();
            print_char(c);
			TxBuffer[k] = c;
	
			if ((TxBuffer[k - 2] == 0x0D) && (TxBuffer[k - 1] == 0x0D) && (TxBuffer[k] == 0x0D)) {
                if ((uart_receive_flag == TURE)) {
    			    uart_receive_flag = FALSE;
					str_cpy(rxBuffer, TxBuffer, k - 2);
					str_set(TxBuffer, 64, 0x00);
					k = 0;
			    }
			} else {
                if (k++ > 64) {
                    k = 0;
                }				
			}
			
            break;
        default: break;
    }
}

#endif

#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
	if (gTimer == 0)     // Timer started by SetTimer() expired?
	{
		TA1CTL &= ~(MC1 + MC0);  // Stop timer.
		TA1CCTL0 &= ~CCIE;       // Disable timer interrupt.
		
		// Disable all interrupts and wake from sleep on exit.
		__bic_SR_register_on_exit(LPM3_bits);
	}
	else   // Timer not yet expired.
	{
		gTimer -= 1;
	}
}

#pragma vector=UNMI_VECTOR
__interrupt void UNMI_ISR(void)
{
    switch (SYSUNIV)
    {
        case 0x0004:  LFXT_Start(XT1DRIVE_3, XCAP_3); break;
        default: break;
    }
}